﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.Orders
{
    public partial class SearchOrders : BasePage
    {
        private static IOrdersBLL ordersBLL = IocContainer.Instance.Resolve<IOrdersBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ControlAuthority(new List<WebControl> { btnDelete,btnhidDelete, btnSearch }, 4);
                this.txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtStartDate.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
            }
        }

        [WebMethod]
        public static string GetOrders(string custName, string status, string orderCode, string startDate, string endDate, string orderField, string orderValue, int pageIndex, int pagePerSize)
        {
            string strResult;
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");

            if (!string.IsNullOrEmpty(custName))
            {
                strWhere.Append(" and tbdc.FName like '%" + custName + "%'");
            }

            if (!string.IsNullOrEmpty(status) && status != "-1")
            {
                strWhere.Append(" and T_Order.Fstatus =" + status);
            }
            if (!string.IsNullOrEmpty(orderCode))
            {
                strWhere.Append(" and T_Order.Fnumber ='" + orderCode + "'");
            }
            if (!string.IsNullOrEmpty(startDate) && string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and T_Order.FUpdateDate >='" + orderCode + "'");
            }
            else if (string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and T_Order.FUpdateDate <='" + endDate.Trim() + " 23:59:59'");
            }
            else if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
            {
                strWhere.Append(" and T_Order.FUpdateDate between '" + startDate + "' and '" + endDate.Trim() + " 23:59:59'");
            }

            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderField) ? " " + orderField : "  FUpdateDate  ");
            strOrderBy.Append(!string.IsNullOrEmpty(orderValue) ? " " + orderValue : "  desc ");

            int totalRecords = 0;
            int totalPages = 0;
            DataTable dtProducts = ordersBLL.GetOrdersByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);

            strResult = SerializationHelper.DataTable2Json(dtProducts);

            return "{\"totalRecords\":\"" + totalRecords.ToString() + "\" ,\"totalPages\":\"" + totalPages.ToString() + "\",\"rows\":" +
                   strResult + " }";

        }

    }
}
